package com.java.basis.collection.set;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * 案例：
 * 要求：使用TreeSet集合实现字符串长度进行排序
 * helloworld zhangsan lisi wangwu beijing xian nanjing
 *
 * @author wangning
 * @create 2021-04-22 22:42
 */
public class TestTreeSet04 {
	public static void main(String[] args) {
		TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				int n1 = o1.length() - o2.length();
				int n2 = o1.compareTo(o2);
				System.out.println("n2 = " + n2);
				return n1 == 0 ? n2 : n1;
			}
		});
		treeSet.add("helloworld");
		treeSet.add("zhangsan");
		treeSet.add("lisi");
		treeSet.add("wangwu");
		treeSet.add("beijing");
		treeSet.add("xian");
		System.out.println(treeSet.toString());
		//自定义排序：[lisi, xian, wangwu, beijing, zhangsan, helloworld]
		//默认排序：[beijing, helloworld, lisi, wangwu, xian, zhangsan]
	}
}
